/*
 * (C) 2005 National Australia Bank
 *
 * [All rights reserved]. This product and related documentation are protected
 * by copyright restricting its use, copying, distribution, and decompilation.
 * No part of this product or related documentation may be reproduced in any
 * form by any means without prior written authorization of
 * National Australia Bank. Unless otherwise arranged, third parties may not
 * have access to this product or related documents.
 */
package au.com.evoCoach.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import au.com.evoCoach.compare.PlayerForRoundComparator;
import au.com.evoCoach.dao.LtiTeamPlayerDAO;
import au.com.evoCoach.dao.PlayerForRoundDAO;
import au.com.evoCoach.dao.RoundDAO;
import au.com.evoCoach.dao.TeamDAO;
import au.com.evoCoach.dao.TeamPlayerDAO;
import au.com.evoCoach.domain.League;
import au.com.evoCoach.domain.LtiTeamPlayer;
import au.com.evoCoach.domain.PlayerForRound;
import au.com.evoCoach.domain.Round;
import au.com.evoCoach.domain.Team;
import au.com.evoCoach.domain.TeamPlayer;
import au.com.evoCoach.dto.TeamDetails;
import au.com.evoCoach.form.TeamSubmissionForm;

@Service
public class TeamService {
	private TeamDAO teamDAO;
	private TeamPlayerDAO teamPlayerDAO;
	private PlayerForRoundDAO playerForRoundDAO;
	private RoundDAO roundDAO;
	private LtiTeamPlayerDAO ltiTeamPlayerDAO;

	@Autowired
	public TeamService(TeamDAO teamDAO, TeamPlayerDAO teamPlayerDAO, PlayerForRoundDAO playerForRoundDAO, RoundDAO roundDAO, LtiTeamPlayerDAO ltiTeamPlayerDAO) {
		this.teamDAO = teamDAO;
		this.teamPlayerDAO = teamPlayerDAO;
		this.playerForRoundDAO = playerForRoundDAO;
		this.roundDAO = roundDAO;
		this.ltiTeamPlayerDAO = ltiTeamPlayerDAO;
	}
	
	public Team authenticateTeam(String coachName, String password) {
		return teamDAO.getTeam(coachName, password);
	}
	
	public List <Team> getTeams(League league) {
		return teamDAO.getTeams(league);
	}
	
	public List <TeamPlayer> getAllTeamDetails() {
		List<TeamPlayer> allTeamPlayers = teamPlayerDAO.getAllTeamPlayers();
		return allTeamPlayers;
	}
	
	public List <TeamDetails> getTeamPlayersList(League league) {
		List <Team> allTeams = teamDAO.getTeams(league);
		List <TeamDetails> test = new ArrayList<TeamDetails>();
		TeamDetails temp;
		
		for (Team myTeam : allTeams)
		{			
			temp = getTeamDetails(myTeam, true);
			test.add(temp);
			
		}
		return test;
	}

	public TeamDetails getTeamDetails(Team team, boolean includeRookies) {
		List<TeamPlayer> teamPlayers = teamPlayerDAO.getTeamPlayers(team, includeRookies);
		List<LtiTeamPlayer> ltis = ltiTeamPlayerDAO.getLtis(team);
		
		for (LtiTeamPlayer ltiPlayer : ltis) {
	        teamPlayers.remove(ltiPlayer.getPlayer());
	        teamPlayers.add(ltiPlayer.getElevatedRookiePlayer());
		}
		
		Round round = roundDAO.getCurrentRound();
	    List<PlayerForRound> players = getPlayersForRound(team, round);
	    
	    List<TeamPlayer> nonSelectedPlayers = new ArrayList<TeamPlayer>();
	    for (TeamPlayer player : teamPlayers) {
	    	boolean foundIt = false;
	    	for (PlayerForRound playerForRound : players) {
	    		if (playerForRound.getTeamPlayer().getPlayer().getId().equals(player.getPlayer().getId())) {
	    			foundIt = true;
	    			break;
	    		}
	    	}
	    	
	    	if (!foundIt) {
	    		nonSelectedPlayers.add(player);
	    	}
	    }
		
        return new TeamDetails(team, teamPlayers, nonSelectedPlayers);
	}
	
	public List<PlayerForRound> getPlayersForRound(Team team, Round round) {
		List<PlayerForRound> players = playerForRoundDAO.getPlayersForRound(team, round);
	    if (players == null || players.size() == 0) {
	        if (round.getRound() > 1) {
	            players = playerForRoundDAO.getPlayersForRound(team, roundDAO.getRound(round.getRound() - 1));
	        }
	    }
	    
	    return players;
	}
	
	public TeamSubmissionForm getTeamSubmissionForm(Team team) {
	    Round round = roundDAO.getCurrentRound();
	    List<PlayerForRound> players = getPlayersForRound(team, round);
	    
	    TeamSubmissionForm teamSubmissionForm = new TeamSubmissionForm();
        Map<String, Integer> playerPositions = new HashMap<String, Integer>();
	    
	    if (players != null && players.size() > 0) {
	        Collections.sort(players, new PlayerForRoundComparator());
	    
            int count = -1;
            String lastPosition = "";
            for (PlayerForRound playerForRound : players) {
                String fieldName = playerForRound.getPlayerPosition().name().substring(0, 1);
                if (!fieldName.equals(lastPosition)) {
                	if (count == 6 && (lastPosition.equals("D") || lastPosition.equals("F"))) {
                		playerPositions.put(lastPosition + "6", -1);
                	}
                	count = 1;
                	lastPosition = fieldName;
                }
                
                if (!fieldName.equals("R")) {
                    fieldName += count;
                }
                else {
                	fieldName = "R1";
                }
                count++;
                
                playerPositions.put(fieldName, playerForRound.getTeamPlayer().getId());
            }
            
            if (count == 6 && (lastPosition.equals("D") || lastPosition.equals("F"))) {
        		playerPositions.put(lastPosition + "6", -1);
        	}
	    }
	    else {
	    	String[] positions = {"D", "M", "F"};
	    	for (int j = 0; j < 3; j++) {
		        for (int i = 1; i <= 6; i++) {
		            playerPositions.put(positions[j] + i, -1);
		        }
	    	}
            playerPositions.put("R1", -1);
	    }
        
        teamSubmissionForm.setPlayerPositions(playerPositions);
        
        return teamSubmissionForm;
	}
}
